package analysishandler

import (
	"github.com/gin-gonic/gin"
	"github.com/shopspring/decimal"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/statistics"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	. "yundian/internel/pkg/dingo/result"
)

// ----------------------- 新统计 handler ------------------ //

// HandleGetUserAnalysisList 用户数据分析列表
func HandleGetUserAnalysisList(ctx *gin.Context) {
	param := &request.UserAnalysisListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewStatisticsDao().GetUserAnalysisList(param)
	lists := data.Unwrap().([]*statistics.StatisticsUserList)

	// 分页信息
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       lists,
	})(OK)
}

// HandleGetUserSettlementAnalysisList 用户数据分析列表-流水排行
func HandleGetUserSettlementAnalysisList(ctx *gin.Context) {
	param := &request.UserAnalysisListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewStatisticsDao().GetUserSettlementAndIncomeRankList(param, "settlement")
	lists := data.Unwrap().([]*statistics.StatisticsUserRankList)

	// 分页信息
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       lists,
	})(OK)
}

// HandleGetUserIncomeAnalysisList 用户数据分析列表-收益排行
func HandleGetUserIncomeAnalysisList(ctx *gin.Context) {
	param := &request.UserAnalysisListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewStatisticsDao().GetUserSettlementAndIncomeRankList(param, "income")
	lists := data.Unwrap().([]*statistics.StatisticsUserRankList)

	// 分页信息
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       lists,
	})(OK)
}

// HandleGetUserTeamAnalysisList 用户数据分析列表-团队排行
func HandleGetUserTeamAnalysisList(ctx *gin.Context) {
	param := &request.UserAnalysisListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	dateStr := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
	data := dao.NewStatisticsDao().GetUserTeamAndPlaceRankList(param, dateStr, "team")
	lists := data.Unwrap().([]*statistics.StatisticsUserRankList)

	// 分页信息
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       lists,
	})(OK)
}

// HandleGetUserPlaceAnalysisList 用户数据分析列表-门店排行
func HandleGetUserPlaceAnalysisList(ctx *gin.Context) {
	param := &request.UserAnalysisListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	dateStr := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
	data := dao.NewStatisticsDao().GetUserTeamAndPlaceRankList(param, dateStr, "place")
	lists := data.Unwrap().([]*statistics.StatisticsUserRankList)

	// 分页信息
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       lists,
	})(OK)
}

// HandleGetUserAnalysisData 获取用户统计列表
func HandleGetUserAnalysisData(ctx *gin.Context)  {
	param := &request.AnalysisUserReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	var data *dingo.ErrorWrap
	if param.StatisticType == "month" {
		data = dao.NewStatisticsDao().GetUserStatisticsMonthDataByUserId(param, param.UserId) // 按月
	} else {
		data = dao.NewStatisticsDao().GetUserStatisticsDayDataByUserId(param, param.UserId) // 按日
	}
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// GetUserSettlementAnalysis 获取用户统计数据分析
func GetUserSettlementAnalysis(ctx *gin.Context)  {
	param := &request.AnalysisUserReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	data := dao.NewStatisticsDao().GetUserSettlementChart(param.UserId, param.StatisticType)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// GetUserSettlementAnalysis 获取用户统计数据分析
func GetUserIncomeAnalysis(ctx *gin.Context)  {
	param := &request.AnalysisUserReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	Response(ctx)("", CodeSuccess, dao.NewStatisticsDao().GetUserIncomeChart(param.UserId, param.StatisticType).Unwrap())(OK)
}

// GetUserPriceAnalysis 获取订单笔数，单图表
func GetUserPriceAnalysis(ctx *gin.Context)  {
	userId, err := helper.GetUserIdFromCtx(ctx)
	isAdmin, _ := helper.GetIsAdminFromCtx(ctx)
	isDraw, _ := helper.GetIsDrawFromCtx(ctx)
	parentId, _ := helper.GetParentIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.AnalysisUserReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	if helper.IsAdminAndStaff(isAdmin, parentId) {
		isDraw = 1
	}

	if param.UserId == 0 { param.UserId = userId }
	Response(ctx)("", CodeSuccess, dao.NewStatisticsDao().GetUserPriceChart(param.UserId, param.StatisticType, isDraw).Unwrap())(OK)
}

// GetUserPriceAnalysis 获取订单笔数，单图表
func GetUserSaleAnalysis(ctx *gin.Context)  {
	param := &request.AnalysisUserReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	Response(ctx)("", CodeSuccess, dao.NewStatisticsDao().GetUserSaleChartByDay(param.UserId, param.StatisticType).Unwrap())(OK)
}

// GetUserAnalysisDetail 获取用户统计详情
func GetUserAnalysisDetail(ctx *gin.Context)  {
	param := &request.AnalysisUserReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewStatisticsDao().GetUserAnalysisDetail(param.UserId, param.StatisticType).Unwrap().(*statistics.UserAnalysisInfo)
	data.UpdateTime = global.Rdb.Get(ctx, "statistics_update_time").Val() // 更新时间
	data.NowDate = time.Now().Format("2006-01-02") + " " + helper.GetWeek(time.Now().Weekday().String())
	dateStr := time.Now().AddDate(0, 0, -1).Format("2006-01-02")

	info := dao.NewStatisticsDao().GetUserAnalysisPlaceTotal(param.UserId, dateStr).Unwrap().(*statistics.UserAnalysisInfo)
	data.TotalPlace = info.TotalPlace // 门店总数
	data.TotalDevices = info.TotalDevices  // 设备总数

	dMoney := dao.NewOrderDao().GetDividendsMoneyByUserId(param.UserId).Unwrap().(int64) // 累计收益
	cMoney := dao.NewOrderDao().GetCashLogByUserId(param.UserId).Unwrap().(int64)        // 提现金额
	data.DividMoney = decimal.NewFromInt(dMoney).Div(decimal.NewFromInt(100)).StringFixed(2)
	data.CashMoney = decimal.NewFromInt(cMoney).Div(decimal.NewFromInt(100)).StringFixed(2)
	data.Money = decimal.NewFromInt(dMoney).Sub(decimal.NewFromInt(cMoney)).Div(decimal.NewFromInt(100)).StringFixed(2)

	Response(ctx)("", CodeSuccess, data)(OK)
}